package com.itboye.eweilisten.me.timu;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.me.timu.adapter.DuiHuaAdapter;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.model.TIbeanTwo;
import com.itboye.eweilisten.model.TiXingBean;
import com.itboye.eweilisten.presenter.TiMuPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.ZuoTiPopupWindow;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 情景对话
 */
public class DuiHuaActivity extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;
    TextView zuoti_title, zuoti_title01;
    TextView zuoti_zidong;
    TextView zuoti_shoudong;

    ZuoTiPopupWindow zuoTiPopupWindow;//弹出框
    DuiHuaAdapter adapter;
    RecyclerView recyclerView;
    TiMuPresenter tiMuPresenter;
    TiXingBean bean;
    List<TIbeanTwo> beanTi = new ArrayList<>();

    ArrayList<ArrayList<String>> questions = new ArrayList<ArrayList<String>>();//内容
    ArrayList<TIbean.Options> answers = new ArrayList<TIbean.Options>();//答案前缀
    ArrayList<String> answerss = new ArrayList<String>();//答案内容

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wanxing);

        tvTitle.setText("微测试");
        bean = new TiXingBean();
        recyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
        tiMuPresenter = new TiMuPresenter(this);
        tiMuPresenter.onXiangQing("171", "40", "itboye");
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                zuoTiPopupWindow = new ZuoTiPopupWindow(DuiHuaActivity.this);
                break;
            case R.id.zuoti_shoudong:
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < beanTi.size(); i++) {
                    sb.append(beanTi.get(i).getTitle() + ":" + beanTi.get(i).getXuan() + ",");
                }
                System.out.println(sb.toString() + "提交答案");
                MAlert.alert(sb.substring(0, sb.length() - 1).toString());

//                String id = (String) SPUtils.get(DuiHuaActivity.this, null, Const.ID, "");
//                showProgressDialog("正在提交,请稍后...", true);
//                tiMuPresenter.onTiJiao(id, "40", "55", sb.substring(0, sb.length() - 1).toString());

                break;
        }
    }

    @Override
    public void update(Observable observable, Object o) {
        ResultEntity entity = handlerError(o);
        if (entity != null) {
            if (entity.getEventType() == tiMuPresenter.zuoti_xq_success) {
                bean = (TiXingBean) entity.getData();

                zuoti_title.setText(bean.getName());
                zuoti_title01.setText(bean.getParent_name());

                List<TiXingBean.ListInfo> list = new ArrayList<TiXingBean.ListInfo>();
                list.addAll(bean.getList());

                for (int i = 0; i < list.size(); i++) {
                    questions = new ArrayList<ArrayList<String>>();
                    answers = new ArrayList<>();
                    answerss = new ArrayList<>();
                    TIbeanTwo b = new TIbeanTwo();
                    //内容
                    for (int i1 = 0; i1 < list.get(i).getContent().size(); i1++) {
                        ArrayList<String> ques = new ArrayList<String>();
                        for (int j = 0; j < list.get(i).getContent().get(i1).size(); j++) {
                            if ("con".equals(list.get(i).getContent().get(i1).get(j)[0] + "")) {
                                if (list.get(i).getContent().get(i1).get(j)[1].equals("nl")) {
                                    ques.add("nl");
                                } else {
                                    ques.add("_");
                                }

                            } else if ("str".equals(list.get(i).getContent().get(i1).get(j)[0] + "")) {
                                //有文字
                                ques.add(list.get(i).getContent().get(i1).get(j)[1] + "");
                            }
                        }
                        questions.add(ques);
                    }

                    b.setContent(questions);
                    //答案前缀
                    for (int j = 0; j < list.get(i).getAnswer().size(); j++) {
                        TIbean.Options op = new TIbean.Options();
                        if ("str".equals(list.get(i).getAnswer().get(j)[1] + "")) {
                            op.setOptions(list.get(i).getAnswer().get(j)[0] + "");
                            op.setDa(false);
                            answers.add(op);
                            //answers.add(list.get(i).getAnswer().get(j)[0] + "");
                        }
                    }
                    b.setOption(answers);
                    //答案内容
                    for (int j = 0; j < list.get(i).getAnswer().size(); j++) {
                        if ("str".equals(list.get(i).getAnswer().get(j)[1] + "")) {
                            answerss.add(list.get(i).getAnswer().get(j)[2] + "");
                        }
                    }
                    b.setAnswers(answerss);
                    b.setAnwser_true(list.get(i).getAnswer_true());
                    b.setTitle(list.get(i).getId());
                    b.setDt_type(list.get(i).getDt_type());

                    beanTi.add(b);
                }
                recyclerView.setLayoutManager(new LinearLayoutManager(this));
                if (adapter == null) {
                    adapter = new DuiHuaAdapter(DuiHuaActivity.this, beanTi);
                    recyclerView.setAdapter(adapter);
                } else {
                    adapter.notifyDataSetChanged();
                }

                CountDownTimer cdt = new CountDownTimer(Integer.parseInt(bean.getTime_limit() + "") * 60 * 1000, 1000) {
                    //                    CountDownTimer cdt = new CountDownTimer(5000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        zuoti_zidong.setText("自动提交：" + millisUntilFinished / 1000 + "″");
                    }

                    @Override
                    public void onFinish() {

                    }
                };

                cdt.start();
            } else if (entity.getEventType() == tiMuPresenter.zuoti_xq_fail) {
                MAlert.alert(entity.getData(), Gravity.TOP);
            }

            if (entity.getEventType() == tiMuPresenter.tijiao_success) {
                MAlert.alert(entity.getData() + "", Gravity.TOP);
            }
            if (entity.getEventType() == tiMuPresenter.tijiao_fail) {
                MAlert.alert(entity.getData() + "", Gravity.TOP);
            }
            try {
                closeProgressDialog();
            } catch (Exception e) {
            }
        }

    }
}
